/***************************************************************************
 *   Copyleft 2008-2009 by												   *
 *	 Iwanicki Krzysztof													   *
 *   <gamma.mini.pw.edu.pl/~iwanickik>									   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/
#include "./vAbstractApp.h"

namespace Vendetta {
	
	vAbstractApp::vAbstractApp() { 
	}

	vAbstractApp::~vAbstractApp() {
		destroyScene();
		mSetupConfig.SaveToFile();
		vEngine3D::InstancePtr()->Kill();
	}

	vVoid vAbstractApp::Run() {
		if (setup()) {
			createScene();
			createCamera();
			vEngine3D::InstancePtr()->Run();
		}
	}

	vBool vAbstractApp::setup() {
		vInt iRet = 1;

		if (mSetupConfig.LoadFromFile() && mSetupConfig.IsSetupValid()) {			
		} else {			
			if (!mSetupDialog.Exec(mSetupConfig.DeviceInfoPtr())) {
				iRet = 0;
			} else {
				mSetupConfig.SetToSave(mSetupDialog.SaveConfig());
			}
		}
		if (iRet) {
			LOGS_INFO("Application started successfully.");
			vEngine3D::InstancePtr()->Init(mSetupConfig.DeviceInfoPtr());
			addFrameListener(this);
			return true;
		}

		return false;		
	}

	vVoid vAbstractApp::addFrameListener(vFrameListener* list) {
		vEngine3D::InstancePtr()->AddFrameListener(list);
	}
}